package cn.yuanqiao.cqbc.tongji.service.impl;
/**
 * @author 赵常飞
 * @date 2024/6/28 9:51
 */

import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.cqbc.common.mapper.MapperToolsMapper;
import cn.yuanqiao.cqbc.tongji.mapper.TongjiMapper;
import cn.yuanqiao.cqbc.tongji.service.ITongjiService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *@Author: 赵常飞
 *@Date: 2024/6/28 9:51
 *@Description:
 *@Note:
 */
@Service
public class TongjiServiceImpl implements ITongjiService {
    @Autowired
    private TongjiMapper tongjiMapper;
    @Autowired
    private MapperToolsMapper DbTool;
    @Override
    public List<Map<String, String>> jstj(JSONObject json) {
        Long categoryId=json.getLong("categoryId");
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        String jssj1=json.getString("jssj1");
        String jssj2=json.getString("jssj2");
        List<Map<String, String>> list= tongjiMapper.jstj(categoryId,tenantCode,jssj1,jssj2);
        if(list.size()==1&&".".equals(list.get(0).get("NAME"))){
            return null;
        }
        return list;
    }

    @Override
    public void exportjstj(JSONObject json, HttpServletResponse response) throws IOException {
        Long categoryId=json.getLong("categoryId");
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        String jssj1=json.getString("jssj1");
        String jssj2=json.getString("jssj2");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("接收统计导出表"+ DateUtils.dateTimeNow("yyyyMMddHHmmss"), "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<List<String>> headList = getexportjstjth(json); //表头
        List<List<String>> dataList = handleDatajstj(tongjiMapper.jstj(categoryId,tenantCode,jssj1,jssj2)); //数据
        EasyExcel.write(response.getOutputStream()).head(headList).sheet("接收统计导出表").doWrite(dataList);
    }

    @Override
    public List<Map<String, String>> cktj(JSONObject json) {
        Long categoryId=json.getLong("categoryId");
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        String jssj1=json.getString("jssj1");
        String jssj2=json.getString("jssj2");
        List<Map<String, String>> list=tongjiMapper.cktj(categoryId,tenantCode,jssj1,jssj2);
        if(list.size()==1&&".".equals(list.get(0).get("NAME"))){
            return null;
        }
        return list;
    }

    @Override
    public void exportcktj(JSONObject json, HttpServletResponse response) throws IOException {
        Long categoryId=json.getLong("categoryId");
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        String jssj1=json.getString("jssj1");
        String jssj2=json.getString("jssj2");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("出库统计导出表"+ DateUtils.dateTimeNow("yyyyMMddHHmmss"), "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<List<String>> headList = getexportcktjth(json); //表头
        List<List<String>> dataList = handleDatacktj(tongjiMapper.cktj(categoryId,tenantCode,jssj1,jssj2)); //数据
        EasyExcel.write(response.getOutputStream()).head(headList).sheet("出库统计导出表").doWrite(dataList);
    }

    @Override
    public List<Map<String, Object>> qztj(JSONObject json) {
        String jssj1=json.getString("jssj1");
        String jssj2=json.getString("jssj2");
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        List<Map<String, Object>> listResult2 = tongjiMapper.getfonds(tenantCode);
        for (int i = 0; i < listResult2.size(); i++) {
            List<Map<String, Object>> datalist=tongjiMapper.getdata(String.valueOf(listResult2.get(i).get("FONDS_CODE")),tenantCode,jssj1,jssj2);
            if(datalist.size()==1&&".".equals(datalist.get(0).get("NAME"))){
                datalist=new ArrayList<>();
            }
            if(datalist.size()>0){
                listResult2.get(i).put("NUM",datalist.size());
                listResult2.get(i).put("ALLSIZE",datalist.get(0).get("HUIZONG"));
            }else{
                listResult2.get(i).put("NUM",0);
                listResult2.get(i).put("ALLSIZE",0);
            }

            listResult2.get(i).put("data",datalist);
        }

        return listResult2;
    }

    @Override
    public void exportqztj(JSONObject json, HttpServletResponse response) throws IOException {
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("全宗统计导出表"+ DateUtils.dateTimeNow("yyyyMMddHHmmss"), "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<List<String>> headList = getexportqztjth(json); //表头
        List<List<String>> dataList = handleDataqztj(qztj(json)); //数据
        EasyExcel.write(response.getOutputStream()).head(headList).sheet("全宗统计导出表").doWrite(dataList);
    }

    private List<List<String>> getexportqztjth(JSONObject json) {
        List<String> headTitle1 = new ArrayList<>();
        List<String> headTitle2 = new ArrayList<>();
        List<String> headTitle3 = new ArrayList<>();
        List<String> headTitle4 = new ArrayList<>();
        List<String> headTitle5 = new ArrayList<>();
        List<String> headTitle6 = new ArrayList<>();
        List<String> headTitle7 = new ArrayList<>();

        headTitle1.add(json.getString("title"));
        headTitle1.add("单位名称:"+json.getString("danwei"));


        headTitle2.add(json.getString("title"));
        headTitle2.add("");


        headTitle3.add(json.getString("title"));
        headTitle3.add("");


        headTitle4.add(json.getString("title"));
        headTitle4.add("");


        headTitle5.add(json.getString("title"));
        headTitle5.add("");


        headTitle6.add(json.getString("title"));
        headTitle6.add("");


        headTitle7.add(json.getString("title"));
        headTitle7.add("日期:"+json.getString("riqi"));


        List<List<String>> headList = new ArrayList<List<String>>(){{
            add(headTitle1);
            add(headTitle2);
            add(headTitle3);
            add(headTitle4);
            add(headTitle5);
            add(headTitle6);
            add(headTitle7);
        }};
        return headList;
    }

    private List<List<String>> handleDataqztj(List<Map<String, Object>> qztj) {
        List<List<String>> list=new ArrayList<List<String>>();
        for (int i = 0; i < qztj.size(); i++) {
            List<String> datalist=new ArrayList<>();
            datalist.add("全宗名称:");
            datalist.add(String.valueOf(qztj.get(i).get("FONDS_NAME")));
            datalist.add("总量:");
            datalist.add(String.valueOf(qztj.get(i).get("NUM")));
            datalist.add("总数:");
            datalist.add(String.valueOf(qztj.get(i).get("ALLSIZE")));
            list.add(datalist);
            List<Map<String,Object>> data =(List<Map<String,Object>>) qztj.get(i).get("data");
            if(data.size()>0){
                List<String> faketh=new ArrayList<String>(){{
                    add("序号");
                    add("标题");
                    add("档号");
                    add("数据包名称");
                    add("数据包大小");
                    add("存储位置");
                    add("出库时间");
                }};
                list.add(faketh);
                for (int j = 0; j < data.size(); j++) {
                    List<String> datai=new ArrayList<String>();
                    datai.add(String.valueOf(data.get(j).get("XH")));
                    datai.add(String.valueOf(data.get(j).get("TITLE")));
                    datai.add(String.valueOf(data.get(j).get("NAME")));
                    datai.add(String.valueOf(data.get(j).get("DH")));
                    datai.add(String.valueOf(data.get(j).get("SIZE")));
                    datai.add(String.valueOf(data.get(j).get("PATH")));
                    datai.add(String.valueOf(data.get(j).get("OUTBOUND_TIME")));
                    list.add(datai);
                }
                List<String> datalast=new ArrayList<String>(){{
                    add("汇总");
                    add("-");
                    add("-");
                    add("-");
                    add(String.valueOf(data.get(0).get("HUIZONG")));
                    add("-");
                    add("-");
                }};
                list.add(datalast);
            }
        }
        return list;
    }


    private List<List<String>> getexportcktjth(JSONObject json) {
        List<String> headTitle1 = new ArrayList<>();
        List<String> headTitle2 = new ArrayList<>();
        List<String> headTitle3 = new ArrayList<>();
        List<String> headTitle4 = new ArrayList<>();
        List<String> headTitle5 = new ArrayList<>();
        List<String> headTitle6 = new ArrayList<>();
        List<String> headTitle7 = new ArrayList<>();

        headTitle1.add(json.getString("title"));
        headTitle1.add("单位名称:"+json.getString("danwei"));
        headTitle1.add("序号");

        headTitle2.add(json.getString("title"));
        headTitle2.add("");
        headTitle2.add("标题");

        headTitle3.add(json.getString("title"));
        headTitle3.add("");
        headTitle3.add("档号");

        headTitle4.add(json.getString("title"));
        headTitle4.add("");
        headTitle4.add("数据包名称");

        headTitle5.add(json.getString("title"));
        headTitle5.add("");
        headTitle5.add("数据包大小");

        headTitle6.add(json.getString("title"));
        headTitle6.add("");
        headTitle6.add("存储位置");

        headTitle7.add(json.getString("title"));
        headTitle7.add("日期:"+json.getString("riqi"));
        headTitle7.add("出库时间");

        List<List<String>> headList = new ArrayList<List<String>>(){{
            add(headTitle1);
            add(headTitle2);
            add(headTitle3);
            add(headTitle4);
            add(headTitle5);
            add(headTitle6);
            add(headTitle7);
        }};
        return headList;
    }

    private List<List<String>> handleDatacktj(List<Map<String, String>> cktj) {

        List<List<String>> list=new ArrayList<>();
        for (int i = 0; i <cktj.size(); i++) {
            List<String> datai=new ArrayList<>();
            datai.add(cktj.get(i).get("XH"));
            datai.add(cktj.get(i).get("TITLE"));
            datai.add(cktj.get(i).get("NAME"));
            datai.add(cktj.get(i).get("DH"));
            datai.add(cktj.get(i).get("SIZE"));
            datai.add(cktj.get(i).get("PATH"));
            datai.add(cktj.get(i).get("OUTBOUND_TIME"));
            list.add(datai);
        }
        List<String> datalast=new ArrayList<String>(){{
            add("汇总");
            add("-");
            add("-");
            add("-");
            add(String.valueOf(cktj.get(0).get("HUIZONG")));
            add("-");
            add("-");
        }};
        list.add(datalast);
        return list;
    }

    private List<List<String>> handleDatajstj(List<Map<String, String>> jstj) {

        List<List<String>> list=new ArrayList<>();
        for (int i = 0; i <jstj.size(); i++) {
                List<String> datai=new ArrayList<>();
                datai.add(jstj.get(i).get("XH"));
                datai.add(jstj.get(i).get("TITLE"));
                datai.add(jstj.get(i).get("NAME"));
                datai.add(jstj.get(i).get("DH"));
                datai.add(jstj.get(i).get("SIZE"));
                datai.add(jstj.get(i).get("PATH"));
                datai.add(jstj.get(i).get("RECEVINGTIME"));
                list.add(datai);
        }
        List<String> datalast=new ArrayList<String>(){{
            add("汇总");
            add("-");
            add("-");
            add("-");
            add(String.valueOf(jstj.get(0).get("HUIZONG")));
            add("-");
            add("-");
        }};
        list.add(datalast);
        return list;
    }

    private List<List<String>> getexportjstjth(JSONObject json) {

        List<String> headTitle1 = new ArrayList<>();
        List<String> headTitle2 = new ArrayList<>();
        List<String> headTitle3 = new ArrayList<>();
        List<String> headTitle4 = new ArrayList<>();
        List<String> headTitle5 = new ArrayList<>();
        List<String> headTitle6 = new ArrayList<>();
        List<String> headTitle7 = new ArrayList<>();

        headTitle1.add(json.getString("title"));
        headTitle1.add("单位名称:"+json.getString("danwei"));
        headTitle1.add("序号");

        headTitle2.add(json.getString("title"));
        headTitle2.add("");
        headTitle2.add("标题");

        headTitle3.add(json.getString("title"));
        headTitle3.add("");
        headTitle3.add("档号");

        headTitle4.add(json.getString("title"));
        headTitle4.add("");
        headTitle4.add("数据包名称");

        headTitle5.add(json.getString("title"));
        headTitle5.add("");
        headTitle5.add("数据包大小");

        headTitle6.add(json.getString("title"));
        headTitle6.add("");
        headTitle6.add("存储位置");

        headTitle7.add(json.getString("title"));
        headTitle7.add("日期:"+json.getString("riqi"));
        headTitle7.add("接收时间");

        List<List<String>> headList = new ArrayList<List<String>>(){{
            add(headTitle1);
            add(headTitle2);
            add(headTitle3);
            add(headTitle4);
            add(headTitle5);
            add(headTitle6);
            add(headTitle7);
        }};
        return headList;
    }
}
